package cn.tedu.boot.demo.controller.handler;

import cn.tedu.boot.demo.ex.ServiceException;
import cn.tedu.boot.demo.web.JsonResult;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(ServiceException.class)
    public JsonResult<Void> handlerServiceException(ServiceException e){
        return JsonResult.fail(e);
    }

    @ExceptionHandler(BindException.class)
    public JsonResult<Void> handlerBindException(BindException e){
        String message = e.getBindingResult().getFieldError().getDefaultMessage();
        return JsonResult.fail(JsonResult.State.ERR_BAD_REQUEST,message);
    }
}
